package org.mullin.stratego.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@Entity
public class Player implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id public String email;
	
	public String name;
	public List<Long> matches = new ArrayList<Long>();
	public List<String> connections = new ArrayList<String>();
	public int numConnections;
	
	public Date lastGameDate = new Date();
	public PRank rank = new PRank();
	
	@SuppressWarnings("unused")
	private Player() { }
	
	public Player(String email) {
		this(email, 1);
	}
	
	public Player(String email, int numConnections) {
		this.email = email;
		this.numConnections = numConnections;
	}
	
	public void addMatch(long matchId) {
		matches.add(matchId);
	}
	
	public boolean removeMatch(long matchId) {
		return matches.remove(matchId);
	}
	
	public List<Long> getMatches() {
		return matches;
	}
	
	public void addConnection() {
		numConnections++;
	}
	
	public void removeConnection() {
		numConnections--;
	}
	
	public void setLastGameDate(Date lastGameDate) {
		this.lastGameDate = lastGameDate;
	}
	
	public Date getLastGameDate() {
		return lastGameDate;
	}
	
	public PRank getRank() {
		return rank;
	}
	
	public void updateRankDeviation() {
		if (lastGameDate != null) {
			rank.updateRatingDeviation(lastGameDate);
		}
	}
	
	public void updateRanking(Player opponent, String winner) {
		rank.updateAfterGame(opponent.getRank().getRatingDeviation(), 
				opponent.getRank().getRating(), winner.equals(email) ? 1 : 0);
	}
}
